package cn.sinata.wanglin.mine.fragment;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.xilada.xldutils.activitys.BaseActivity;
import com.xilada.xldutils.adapter.BaseRecyclerAdapter;
import com.xilada.xldutils.adapter.DividerGridItemDecoration;
import com.xilada.xldutils.adapter.util.ViewHolder;
import com.xilada.xldutils.fragment.BaseLazyFragment;
import com.xilada.xldutils.utils.ActivityUtil;
import com.xilada.xldutils.utils.DensityUtil;
import com.xilada.xldutils.utils.SharedPreferencesUtils;
import com.xilada.xldutils.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sinata.wanglin.R;
import cn.sinata.wanglin.commonutils.Constant;
import cn.sinata.wanglin.mine.activity.MoreSayingAndBaguaActivity;
import cn.sinata.wanglin.model.Circle;
import cn.sinata.wanglin.model.Comment;
import cn.sinata.wanglin.model.ImgBean;
import cn.sinata.wanglin.model.Like;
import cn.sinata.wanglin.model.LikeData;
import cn.sinata.wanglin.model.Makecomplaints;
import cn.sinata.wanglin.model.MyDynamicInfo;
import cn.sinata.wanglin.neighbor.activity.EditCommentActivity;
import cn.sinata.wanglin.neighbor.activity.MineCircleActivity;
import cn.sinata.wanglin.neighbor.controller.FriendCircleListener;
import cn.sinata.wanglin.neighbor.view.UserCircleView;
import cn.sinata.wanglin.net.HttpManager;
import cn.sinata.wanglin.net.model.util.ResultDataSubscriber;
import cn.sinata.wanglin.village.activity.AllUserInfoActivity;
import cn.sinata.wanglin.village.activity.EditCommentOtherActivity;
import cn.sinata.wanglin.village.activity.MakecomplaintsdetailActivity;
/**
 * description：我的---圈子八卦
 * Created by kaihongwu on 2016/12/06 上午 9:45
 */
public class QuanzibaguaFragment extends BaseLazyFragment implements AppBarLayout.OnOffsetChangedListener{
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    //吐槽
    @BindView(R.id.tv_sayingcontent)
    TextView tv_sayingcontent;//
    @BindView(R.id.tv_sayingseenum)
    TextView tv_sayingseenum;//
    @BindView(R.id.tv_sayingcommentnum)
    TextView tv_sayingcommentnum;//
    @BindView(R.id.tv_sayingzannum)
    TextView tv_sayingzannum;//
    @BindView(R.id.tv_sayingcommentshow1)
    TextView tv_sayingcommentshow1;//
    @BindView(R.id.tv_sayingcommentshow2)
    TextView tv_sayingcommentshow2;//
    @BindView(R.id.rv_sayingshowphoto)
    RecyclerView rv_sayingshowphoto;//
    @BindView(R.id.tv_sayingmorecomment)
    TextView tv_sayingmorecomment;//
    @BindView(R.id.ly_commentsaying)
    LinearLayout ly_commentsaying;//
    @BindView(R.id.iv_comment_boxtopsaying)
    ImageView iv_comment_boxtopsaying;//

    //八卦
    @BindView(R.id.iv_comment_boxtopbagua)
    ImageView iv_comment_boxtopbagua;//
    @BindView(R.id.tv_baguacontent)
    TextView tv_baguacontent;//
    @BindView(R.id.tv_baguaseenum)
    TextView tv_baguaseenum;//
    @BindView(R.id.tv_baguacommentnum)
    TextView tv_baguacommentnum;//
    @BindView(R.id.tv_baguazannum)
    TextView tv_baguazannum;//
    @BindView(R.id.tv_baguacommentshow1)
    TextView tv_baguacommentshow1;//
    @BindView(R.id.tv_baguacommentshow2)
    TextView tv_baguacommentshow2;//
    @BindView(R.id.rv_baguashowphoto)
    RecyclerView rv_baguashowphoto;//
    @BindView(R.id.tv_baguamorecomment)
    TextView tv_baguamorecomment;//
    private int imgwith=80;
    @BindView(R.id.ly_saying)
    LinearLayout ly_saying;//吐槽隐藏布局
    @BindView(R.id.ly_bagua)
    LinearLayout ly_bagua;//八卦隐藏布局
    @BindView(R.id.tv_morebagua)
    TextView tv_morebagua;//
    @BindView(R.id.tv_moresaying)
    TextView tv_moresaying;//
    @BindView(R.id.tv_morecircle)
    TextView tv_morecircle;//
    @BindView(R.id.ly_commentbagua)
    LinearLayout ly_commentbagua;//
    @BindView(R.id.userCircleView)
    UserCircleView userCircleView;//
    private String userid;
    private DividerGridItemDecoration mDividerGridItemDecoration;
    private Circle mCircle;

    @Override
    protected int getContentViewLayoutID() {

        return  R.layout.fragment_quanzibagua;
    }
public static QuanzibaguaFragment newInstance(String userid){
    QuanzibaguaFragment mQuanzibaguaFragment=new QuanzibaguaFragment();
    Bundle bundle = new Bundle();
    bundle.putString("userid",userid);
    mQuanzibaguaFragment.setArguments(bundle);
    return mQuanzibaguaFragment;
}
    @Override
    protected void onFirstVisibleToUser() {

        addOnOffsetChangeListener();

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMyDynamic();
            }
        });

        if(getArguments()!=null){
            userid=getArguments().getString("userid");
        }
        imgwith=(DensityUtil.getDeviceWidth(context)-DensityUtil.dip2px(context,30))/3;
        mDividerGridItemDecoration=new DividerGridItemDecoration(context);

        userCircleView.setListener(new FriendCircleListener() {
            @Override
            public void showAllToggle() {
                userCircleView.update(0,mCircle,mCircle.getTime());
            }

            @Override
            public void onAvatarClick(int position, String id) {

            }

            @Override
            public void onCommentButtonClick(int position, Comment comment) {
                if (mCircle == null) {
                    return;
                }
                ActivityUtil.create(QuanzibaguaFragment.this)
                        .go(EditCommentActivity.class)
                        .put("id",mCircle.getId())
                        .put("toUser",comment.getId())
                        .put("name",comment.getFromNick())
                        .put("commentId",comment.getCommentId())
                        .startForResult(13);
            }

            @Override
            public void onPraiseButtonClick(int position, String id) {
                if (mCircle == null) {
                    return;
                }
                praise(mCircle,id);
            }

            @Override
            public void onCommentItemClick(int position, Comment comment) {
                if (mCircle == null) {
                    return;
                }
                ActivityUtil.create(QuanzibaguaFragment.this)
                        .go(EditCommentActivity.class)
                        .put("id",mCircle.getId())
                        .put("toUser",comment.getId())
                        .put("name",comment.getFromNick())
                        .put("commentId",comment.getCommentId())
                        .put("type",1)
                        .startForResult(13);
            }

            @Override
            public void onLinkNameClick(String id) {

            }

            @Override
            public void onDelete(int position, String id) {
                if (mCircle == null) {
                    return;
                }
                deleteCircle(id);
            }
        });
        getMyDynamic();

    }

    private void getMyDynamic() {
        String myUserId=SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.getMyDynamic(userid,myUserId).subscribe(new ResultDataSubscriber<MyDynamicInfo>(this) {
            @Override
            public void onSuccess(String msg, MyDynamicInfo myDynamicInfo) {
                mSwipeRefreshLayout.setRefreshing(false);
                if(myDynamicInfo!=null){
                    if (myDynamicInfo.getCircle() != null) {
                        mCircle = myDynamicInfo.getCircle();
                        userCircleView.setVisibility(View.VISIBLE);
                        tv_morecircle.setText("更多"+mCircle.getCountNum()+"条圈子>>");
                        userCircleView.update(0,mCircle,mCircle.getTime());
                    } else {
                        userCircleView.setVisibility(View.GONE);
                        tv_morecircle.setText("更多0条圈子>>");
                    }
                    //吐槽
                    if(myDynamicInfo.getComplaints()!=null){
                        ly_saying.setVisibility(View.VISIBLE);
                        setDataFromComplaints(myDynamicInfo.getComplaints());
                    }else {
                        ly_saying.setVisibility(View.GONE);
                    }
                    //八卦
                    if(myDynamicInfo.getGossipComment()!=null){
                        ly_bagua.setVisibility(View.VISIBLE);
                        setDataFromGossipComment(myDynamicInfo.getGossipComment());
                    }else {
                        ly_bagua.setVisibility(View.GONE);
                    }
                }

            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    /**
     * 绑定同城八卦数据
     * @param gossipComment
     */

    private void setDataFromGossipComment(final MyDynamicInfo.GossipCommentBean gossipComment) {
        if(!TextUtils.isEmpty(gossipComment.getCountNum())){
            tv_morebagua.setText("更多"+gossipComment.getCountNum()+"条八卦>>");
        }else{
            tv_morebagua.setText("更多0条八卦");
        }
        if(!TextUtils.isEmpty(gossipComment.getContent())){
            tv_baguacontent.setText(EaseSmileUtils.getSmiledText(context,gossipComment.getContent()));
        }else{
            tv_baguacontent.setText("暂无发布内容");
        }
        //浏览量
        if(!TextUtils.isEmpty(gossipComment.getBrowses())){
            tv_baguaseenum.setText(gossipComment.getBrowses());
        }else{
            tv_baguaseenum.setText("0");
        }
        //评论量
        if(!TextUtils.isEmpty(gossipComment.getComments())){
            if(Integer.parseInt(gossipComment.getComments())>2){
                tv_baguamorecomment.setVisibility(View.VISIBLE);
            }else{
                tv_baguamorecomment.setVisibility(View.GONE);
            }
            tv_baguacommentnum.setText(gossipComment.getComments());
        }else{
            tv_baguacommentnum.setText("0");
            tv_baguamorecomment.setVisibility(View.GONE);
        }
        //当前用户是否点赞 1：点过 0：没点
        if(!TextUtils.isEmpty(gossipComment.getClickPraise())&&"1".equals(gossipComment.getClickPraise())){
            Resources res = context.getResources();
            Drawable img_off = res.getDrawable(R.mipmap.ic_zannum2);
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
            tv_baguazannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
            tv_baguazannum.setTextColor(context.getResources().getColor(R.color.app_theme_color));
        }else{
            Resources res = context.getResources();
            Drawable img_off = res.getDrawable(R.mipmap.ic_zannum1);
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
            tv_baguazannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
            tv_baguazannum.setTextColor(context.getResources().getColor(R.color.textColor66));

        }
        //点赞量
        if(!TextUtils.isEmpty(gossipComment.getLikes())){
            tv_baguazannum.setText(gossipComment.getLikes());
        }else{
            tv_baguazannum.setText("0");
        }
        if(gossipComment.getCommentList()!=null&&gossipComment.getCommentList().size()!=0){
            if(gossipComment.getCommentList().get(0)!=null) {
                //评论1
                if(!TextUtils.isEmpty(gossipComment.getCommentList().get(0).getUserName())){
                    String commentPerson=gossipComment.getCommentList().get(0).getUserName()+":";
                    String allcomment=commentPerson+gossipComment.getCommentList().get(0).getContent();
                    StyleSpan boldSpan=new StyleSpan(Typeface.BOLD);
                    SpannableStringBuilder builder = new SpannableStringBuilder(allcomment);
                    builder.setSpan(boldSpan,0,commentPerson.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

                    Spannable span = EaseSmileUtils.getSmiledText(context,builder);
                    tv_baguacommentshow1.setText(span, TextView.BufferType.SPANNABLE);
                    tv_baguacommentshow1.setVisibility(View.VISIBLE);
                    ly_commentbagua.setVisibility(View.VISIBLE);
                    iv_comment_boxtopbagua.setVisibility(View.VISIBLE);

                }else{
                    tv_baguacommentshow1.setText("");
                    tv_baguacommentshow1.setVisibility(View.GONE);
                    ly_commentbagua.setVisibility(View.GONE);
                    iv_comment_boxtopbagua.setVisibility(View.GONE);
                }
            }
            if(gossipComment.getCommentList().size()>1&&gossipComment.getCommentList().get(1)!=null) {
                //评论2
                if(!TextUtils.isEmpty(gossipComment.getCommentList().get(1).getUserName())){
                    String commentPerson=gossipComment.getCommentList().get(1).getUserName()+":";
                    String allcomment=commentPerson+gossipComment.getCommentList().get(1).getContent();
                    StyleSpan boldSpan=new StyleSpan(Typeface.BOLD);
                    SpannableStringBuilder builder = new SpannableStringBuilder(allcomment);
                    builder.setSpan(boldSpan,0,commentPerson.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

                    Spannable span = EaseSmileUtils.getSmiledText(context,builder);
                    tv_baguacommentshow2.setText(span, TextView.BufferType.SPANNABLE);
                    tv_baguacommentshow2.setVisibility(View.VISIBLE);



                }else{
                    tv_baguacommentshow2.setText("");
                    tv_baguacommentshow2.setVisibility(View.GONE);
                }

            }
      }


        GridLayoutManager mgr=new GridLayoutManager(context,3);
        rv_baguashowphoto.removeItemDecoration(mDividerGridItemDecoration);
        rv_baguashowphoto.addItemDecoration(mDividerGridItemDecoration);
        rv_baguashowphoto.setLayoutManager(mgr);
        BaseRecyclerAdapter showphotoAdapter=new BaseRecyclerAdapter<ImgBean>(gossipComment.getImgList(),R.layout.item_showphoto) {
            @Override
            public void onBind(final int position, final ImgBean data, ViewHolder holder) {
                if(gossipComment.getImgList().size()!=1){
                    holder.bind(R.id.iv_showphoto).getLayoutParams().width=imgwith;
                    holder.bind(R.id.iv_showphoto).getLayoutParams().height=imgwith;
                    ((SimpleDraweeView) holder.bind(R.id.iv_showphoto)).setImageURI(data.getImgUrl());
                }else{
                    holder.bind(R.id.iv_showphoto).getLayoutParams().width=imgwith*2;
                    holder.bind(R.id.iv_showphoto).getLayoutParams().height=imgwith*2;
                    ((SimpleDraweeView) holder.bind(R.id.iv_showphoto)).setImageURI(data.getImgUrl());
                }
            }
        };
        rv_baguashowphoto.setAdapter(showphotoAdapter);
        showphotoAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(context).go(MakecomplaintsdetailActivity.class)
                        .put("type","2")
                        .put("id",gossipComment.getId())
                        .start();
            }
        });
        tv_baguacontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(context).go(MakecomplaintsdetailActivity.class)
                        .put("type","2")
                        .put("id",gossipComment.getId())
                        .start();
            }
        });
        tv_baguamorecomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(context).go(MakecomplaintsdetailActivity.class)
                        .put("type","2")
                        .put("id",gossipComment.getId())
                        .start();
            }
        });
        tv_baguazannum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                praise("A",gossipComment.getId());
            }
        });
        tv_baguacommentnum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(QuanzibaguaFragment.this)
                        .go(EditCommentOtherActivity.class)
                        .put("id",gossipComment.getId())
                        .put("toUser","-1")
                        .put("externalType","A")
                        .startForResult(12);
            }
        });

    }

    /**
     * 绑定吐槽数据
     * @param complaints
     */

    private void setDataFromComplaints(final MyDynamicInfo.ComplaintsBean complaints) {
        if(!TextUtils.isEmpty(complaints.getCountNum())){
            tv_moresaying.setText("更多"+complaints.getCountNum()+"条吐槽>>");
        }else{
            tv_moresaying.setText("更多0条吐槽");
        }

        if(!TextUtils.isEmpty(complaints.getContent())){
            tv_sayingcontent.setText(EaseSmileUtils.getSmiledText(context,complaints.getContent()));
        }else{
            tv_sayingcontent.setText("暂无发布内容");
        }
        //浏览量
        if(!TextUtils.isEmpty(complaints.getBrowses())){
            tv_sayingseenum.setText(complaints.getBrowses());
        }else{
            tv_sayingseenum.setText("0");
        }
        //评论量
        if(!TextUtils.isEmpty(complaints.getComments())){
            if(Integer.parseInt(complaints.getComments())>2){
                tv_sayingmorecomment.setVisibility(View.VISIBLE);
            }else{
                tv_sayingmorecomment.setVisibility(View.GONE);
            }
            tv_sayingcommentnum.setText(complaints.getComments());
        }else{
            tv_sayingmorecomment.setVisibility(View.GONE);
            tv_sayingcommentnum.setText("0");
        }

        //当前用户是否点赞 1：点过 0：没点
        if(!TextUtils.isEmpty(complaints.getClickPraise())&&"1".equals(complaints.getClickPraise())){
            Resources res = context.getResources();
            Drawable img_off = res.getDrawable(R.mipmap.ic_zannum2);
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
            tv_sayingzannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
            tv_sayingzannum.setTextColor(context.getResources().getColor(R.color.app_theme_color));
        }else{
            Resources res = context.getResources();
            Drawable img_off = res.getDrawable(R.mipmap.ic_zannum1);
            // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
            img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
            tv_sayingzannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
            tv_sayingzannum.setTextColor(context.getResources().getColor(R.color.textColor66));

        }

        //点赞量
        if(!TextUtils.isEmpty(complaints.getLikes())){
            tv_sayingzannum.setText(complaints.getLikes());
        }else{
            tv_sayingzannum.setText("0");
        }
        if(complaints.getCommentList()!=null&&complaints.getCommentList().size()!=0){
            if(complaints.getCommentList().get(0)!=null){
                //评论1
                if(!TextUtils.isEmpty(complaints.getCommentList().get(0).getUserName())){
                    String commentPerson=complaints.getCommentList().get(0).getUserName()+":";
                    String allcomment=commentPerson+complaints.getCommentList().get(0).getContent();
                    StyleSpan boldSpan=new StyleSpan(Typeface.BOLD);
                    SpannableStringBuilder builder = new SpannableStringBuilder(allcomment);
                    builder.setSpan(boldSpan,0,commentPerson.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

                    Spannable span = EaseSmileUtils.getSmiledText(context,builder);
                    tv_sayingcommentshow1.setText(span, TextView.BufferType.SPANNABLE);
                    tv_sayingcommentshow1.setVisibility(View.VISIBLE);
                    ly_commentsaying.setVisibility(View.VISIBLE);
                    iv_comment_boxtopsaying.setVisibility(View.VISIBLE);

                }else{
                    tv_sayingcommentshow1.setText("");
                    tv_sayingcommentshow1.setVisibility(View.GONE);
                    ly_commentsaying.setVisibility(View.GONE);
                    iv_comment_boxtopsaying.setVisibility(View.GONE);
                }
            }
                if(complaints.getCommentList().size()>1&&null!=complaints.getCommentList().get(1)){
                    //评论2
                    if(!TextUtils.isEmpty(complaints.getCommentList().get(1).getUserName())){
                        String commentPerson=complaints.getCommentList().get(1).getUserName()+":";
                        String allcomment=commentPerson+complaints.getCommentList().get(1).getContent();
                        StyleSpan boldSpan=new StyleSpan(Typeface.BOLD);
                        SpannableStringBuilder builder = new SpannableStringBuilder(allcomment);
                        builder.setSpan(boldSpan,0,commentPerson.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

                        Spannable span = EaseSmileUtils.getSmiledText(context,builder);
                        tv_sayingcommentshow2.setText(span, TextView.BufferType.SPANNABLE);
                        tv_sayingcommentshow2.setVisibility(View.VISIBLE);


                    }else{
                        tv_sayingcommentshow2.setText("");
                        tv_sayingcommentshow2.setVisibility(View.GONE);

                    }
             }
        }

        GridLayoutManager mgr=new GridLayoutManager(context,3);
        rv_sayingshowphoto.removeItemDecoration(mDividerGridItemDecoration);
        rv_sayingshowphoto.addItemDecoration(mDividerGridItemDecoration);
        rv_sayingshowphoto.setLayoutManager(mgr);
        Utils.systemErr("---complaints.getImgList()---"+complaints.getImgList().toString());
        BaseRecyclerAdapter showphotoAdapter=new BaseRecyclerAdapter<ImgBean>(complaints.getImgList(),R.layout.item_showphoto) {
            @Override
            public void onBind(final int position, final ImgBean data, ViewHolder holder) {
                if(complaints.getImgList().size()!=1){
                    holder.bind(R.id.iv_showphoto).getLayoutParams().width=imgwith;
                    holder.bind(R.id.iv_showphoto).getLayoutParams().height=imgwith;
                    ((SimpleDraweeView) holder.bind(R.id.iv_showphoto)).setImageURI(data.getImgUrl());
                }else{
                    holder.bind(R.id.iv_showphoto).getLayoutParams().width=imgwith*2;
                    holder.bind(R.id.iv_showphoto).getLayoutParams().height=imgwith*2;
                    ((SimpleDraweeView) holder.bind(R.id.iv_showphoto)).setImageURI(data.getImgUrl());
                }

            }
        };
        rv_sayingshowphoto.setAdapter(showphotoAdapter);
        showphotoAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                ActivityUtil.create(context).go(MakecomplaintsdetailActivity.class)
                        .put("type","1")
                        .put("id",complaints.getId())
                        .start();
            }
        });
        tv_sayingcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(context).go(MakecomplaintsdetailActivity.class)
                        .put("type","1")
                        .put("id",complaints.getId())
                        .start();
            }
        });
        tv_sayingmorecomment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(context).go(MakecomplaintsdetailActivity.class)
                        .put("type","1")
                        .put("id",complaints.getId())
                        .start();
            }
        });
        tv_sayingzannum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                praise("B",complaints.getId());
            }
        });
        tv_sayingcommentnum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActivityUtil.create(QuanzibaguaFragment.this)
                        .go(EditCommentOtherActivity.class)
                        .put("id",complaints.getId())
                        .put("toUser","-1")
                        .put("externalType","B")
                        .startForResult(12);
            }
        });
    }

    @Override
    protected void onVisibleToUser() {
        addOnOffsetChangeListener();
    }

    @Override
    protected void onInvisibleToUser() {
        removeOnOffsetChangeListener();
    }

    @OnClick({R.id.tv_moresaying, R.id.tv_morebagua,R.id.tv_morecircle})
    void onClick(View v) {
        switch (v.getId()) {
//            1 吐槽 2八卦
            case R.id.tv_moresaying:
                //描述:更多的吐槽
                ActivityUtil.create(context).go(MoreSayingAndBaguaActivity.class)
                        .put("type",1)
                        .put("userid",userid)
                        .start();
                break;
            case R.id.tv_morebagua:
                //描述:更多的八卦
                //描述:
                ActivityUtil.create(context).go(MoreSayingAndBaguaActivity.class)
                        .put("type",2)
                        .put("userid",userid)
                        .start();
                break;
            case R.id.tv_morecircle:
                ActivityUtil.create(context).go(MineCircleActivity.class)
                        .put("id",userid)
                        .start();
                break;
        }
    }
    private void praise(final String externalType, String id){
        String userid= SharedPreferencesUtils.getString(Constant.SP_USERID);
        HttpManager.clickPraise(userid,externalType,id)
                .subscribe(new ResultDataSubscriber<LikeData>(this) {
                    @Override
                    public void onSuccess(String msg, LikeData likeData) {
                        showToast(msg);
                        if("A".equals(externalType)){
                            int num= Integer.parseInt(tv_baguazannum.getText().toString());
                            //当前用户行为 1：取消点赞 0：点赞
                            if (likeData.getClickPraise() == 0) {
                                num++;
                                tv_baguazannum.setText(""+num);
                                Resources res = getResources();
                                Drawable img_off = res.getDrawable(R.mipmap.ic_zannum2);
                                // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
                                img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                                tv_baguazannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
                                tv_baguazannum.setTextColor(getResources().getColor(R.color.app_theme_color));
                            } else {
                                num--;
                                tv_baguazannum.setText(""+num);
                                Resources res = getResources();
                                Drawable img_off = res.getDrawable(R.mipmap.ic_zannum1);
                                // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
                                img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                                tv_baguazannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
                                tv_baguazannum.setTextColor(getResources().getColor(R.color.textColorHint));
                            }
                        }else if ("B".equals(externalType)){
                            int num= Integer.parseInt(tv_sayingzannum.getText().toString());
                            //当前用户行为 1：取消点赞 0：点赞
                            if (likeData.getClickPraise() == 0) {
                                num++;
                                tv_sayingzannum.setText(""+num);
                                Resources res = getResources();
                                Drawable img_off = res.getDrawable(R.mipmap.ic_zannum2);
                                // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
                                img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                                tv_sayingzannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
                                tv_sayingzannum.setTextColor(getResources().getColor(R.color.app_theme_color));
                            } else {
                                num--;
                                tv_sayingzannum.setText(""+num);
                                Resources res = getResources();
                                Drawable img_off = res.getDrawable(R.mipmap.ic_zannum1);
                                // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
                                img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
                                tv_sayingzannum.setCompoundDrawables(img_off, null, null, null); //设置右图标
                                tv_sayingzannum.setTextColor(getResources().getColor(R.color.textColorHint));
                            }
                        }

                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==-1){
            switch (requestCode){
                case 12:
                    showDialog();
                    getMyDynamic();
                    break;
                case 13:
                    if (data != null) {
                        Comment comment = (Comment) data.getSerializableExtra("data");
                        if (mCircle.getCommentList() == null) {
                            return;
                        }
                        mCircle.getCommentList().add(comment);
                        int commentNum = mCircle.getCommentNum();
                        mCircle.setCommentNum(commentNum+1);
                        userCircleView.update(0, mCircle, mCircle.getTime());
                    }
                    break;
            }

        }
    }

    private void praise(final Circle circle, String id){
        showDialog();
        HttpManager.clickPraise(userid,"C",id)
                .subscribe(new ResultDataSubscriber<LikeData>(this) {
                    @Override
                    public void onSuccess(String msg, LikeData likeData) {
                        showToast(msg);
                        //当前用户行为 1：取消点赞 0：点赞
                        if (likeData.getClickPraise() == 0) {
                            int num = circle.getLikeNum();
                            if (num < 4) {
                                circle.getLikeList().add(likeData.getLike());
                            }
                            num++;
                            circle.setLikeNum(num);
                            circle.setClickPraise(1);
                        } else {
                            int num = circle.getLikeNum();
                            List<Like> likes = circle.getLikeList();
                            for (Like like : likes) {
                                if (like.getUserID().equals(userid)) {
                                    likes.remove(like);
                                    break;
                                }
                            }
                            num--;
                            if (num < 0) {
                                num = 0;
                            }
                            circle.setLikeNum(num);
                            circle.setClickPraise(0);
                        }
                        userCircleView.update(0,circle,circle.getTime());
                    }
                });
    }

    private void deleteCircle( String id){

        showDialog();
        HttpManager.delSpoke(id,"C")
                .subscribe(new ResultDataSubscriber<JsonObject>(this) {
                    @Override
                    public void onSuccess(String msg, JsonObject jsonObject) {
                        showDialog();
                        getMyDynamic();
                    }
                });
    }

    void addOnOffsetChangeListener(){
        if (getActivity() instanceof AllUserInfoActivity) {
            ((AllUserInfoActivity) getActivity()).addOnOffsetChangedListener(this);
        } else if (getParentFragment()!=null && getParentFragment() instanceof MineFragment) {
            ((MineFragment) getParentFragment()).addOnOffsetChangedListener(this);
        }
    }

    void removeOnOffsetChangeListener(){
        if (getActivity() instanceof AllUserInfoActivity) {
            ((AllUserInfoActivity) getActivity()).removeOnOffsetChangedListener(this);
        }else if (getParentFragment()!=null && getParentFragment() instanceof MineFragment) {
            ((MineFragment) getParentFragment()).removeOnOffsetChangedListener(this);
        }
    }

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (verticalOffset >= 0) {
            mSwipeRefreshLayout.setEnabled(true);
        } else {
            mSwipeRefreshLayout.setEnabled(false);
        }
    }
}
